package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.model.Owner;
import com.woniuxy.result.Result;
import com.woniuxy.service.OwnerService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("owner")
public class OwnerController {

    @Resource
    private OwnerService ownerService;

    //查询所有业主的信息
    @GetMapping("list")
    public Result getAll(){
        List<Owner> list = ownerService.list();
        return Result.success(list);
    }

    //分页查询,可以根据名称模糊匹配
    @GetMapping("page")
    public Result pageQuery(@RequestParam Integer page, @RequestParam Integer pageSize, String userName){
        QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
        if(userName!=null&&!userName.isEmpty()){
            //like匹配
            queryWrapper.like("username",userName);
        }
        Page<Owner> iPage = new Page<>(page,pageSize);
        List<Owner> list = ownerService.list(iPage,queryWrapper);
//        System.out.println(list);
        return Result.success(list);
    }

    //插入一条数据
    @PostMapping("insert")
    public Result addOwner(@RequestBody Owner owner){
        owner.setDeltime(LocalDateTime.now());
        ownerService.save(owner);
        return Result.success();
    }

    //删除
    @DeleteMapping("delete/{id}")
    public Result removeOwner(@PathVariable Integer id){
        ownerService.removeById(id);
        return Result.success().msg("删除成功");
    }
    //修改数据
    @PutMapping("update")
    public Result updateById(@RequestBody Owner owner){
        ownerService.updateById(owner);
        return Result.success().msg("修改信息成功");
    }

}
